package com.imucs.campus_teacher.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imucs.campus_teacher.entity.Student;
import com.imucs.campus_teacher.service.StudentService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.IdWorker;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述:
 *
 * @author chenconghua
 * @version 1.0
 * @create 2022-09-27 11:41 PM
 */

@RestController
@CrossOrigin
@RequestMapping("/student")
public class StudentController {


    @Autowired
    private StudentService studentService;


    @PostMapping("/login")
    public Result login(@RequestBody Map m ){
        Integer studentId = (Integer) m.get("student_id");
        String password = (String) m.get("password");

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);
        Student student = studentService.getOne(queryWrapper);
        if (student !=null){
            if (student.getPassword().equals(password)){
                return new Result(true,StatusCode.OK,"成功", student);
            }else{
                return new Result(false,StatusCode.ERROR,"密码错误");
            }

        }else{
            return new Result(false,StatusCode.ERROR,"用户名不存在");
        }
    }

    @GetMapping("/list")
    public Result list(){

        return new Result(true,StatusCode.OK,"查询成功", studentService.list());
    }

    /**
     * 多条件读取数据
     * @param m
     * @return
     */
    @PostMapping("/selectBy")
    public Result selectBy(@RequestBody Map m){

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        // 数据判断的时候用String，使用的时候强转
        if (null!=(Integer) m.get("student_id")){
            Integer studentId=(Integer) m.get("student_id");
            queryWrapper.eq("student_id",studentId);
        }
        if (null!=(String) m.get("realname")){
            String realname=(String) m.get("realname");
            queryWrapper.eq("realname",realname);
        }
        if (null!=(Integer) m.get("mobile")){
            Integer mobile=(Integer) m.get("mobile");
            queryWrapper.eq("mobile",mobile);
        }
        if (null!=(String) m.get("email")){
            String email=(String) m.get("email");
            queryWrapper.eq("email",email);
        }
        if (null!=(String) m.get("status")){
            String status=(String) m.get("status");
            queryWrapper.eq("status",status);
        }

        List<Student> teacherlist = studentService.list(queryWrapper);
        if (0!=teacherlist.size()){
            return new Result(true,StatusCode.OK,"查询成功",teacherlist);
        }else{
            return new Result(false,StatusCode.ERROR,"查询失败");
        }

    }


    /**
     * 根据学号删除数据
     * @param m
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(@RequestBody Map m){
        Integer studentId = (Integer) m.get("student_id");
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);

        if (studentService.remove(queryWrapper)){
            return new Result(true,StatusCode.OK,"删除成功");
        }else{
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
    }

    /**
     * 学生学号、密码是必须填写的，
     * 其他选填
     * @param m
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Map m){

        Integer studentId=(Integer) m.get("student_id");
        String password =(String) m.get("password");
        Student student = new Student();
        student.setStudentId(studentId);
        student.setPassword(password);
        if (null!=(String)m.get("realname")){
            student.setRealname((String) m.get("realname"));
        }
        if (null!=(Integer) m.get("mobile")){
            student.setMobile(Integer.valueOf((String) m.get("mobile")));
        }

        if (null!=(String) m.get("email")){
            student.setEmail((String) m.get("email"));
        }
        student.setId(new IdWorker().nextId()+"");

        student.setCreateDate(new Date());

        student.setStatus("1");
        if (studentService.saveOrUpdate(student)) {
            return new Result(true,StatusCode.OK,"添加成功");
        }else {
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
    }

    /**
     * 根据学生的学号
     * 修改密码、姓名、电话、身份、邮箱
     * @param m
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody Map m){

        String studentId = (String) m.get("student_id");
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id",studentId);
        Student student = studentService.getOne(queryWrapper);

        if (null!= student){
            if (null!=(String)m.get("password")){

                student.setPassword((String)m.get("password"));
            }
            if (null!=(String)m.get("realname")){

                student.setRealname((String)m.get("realname"));
            }
            // 按照Integer 类型传入
            if (null!=(Integer) m.get("mobile")){
                student.setMobile((Integer) m.get("mobile"));
            }
            if (null!=(String)m.get("status")){

                student.setStatus((String) m.get("status"));
            }
            if (null!=(String)m.get("email")){

                student.setEmail((String) m.get("email"));
            }

            if (studentService.saveOrUpdate(student)) {
                return new Result(true,StatusCode.OK,"修改成功");
            }else{
                return new Result(false,StatusCode.ERROR,"修改失败");
            }

        }else{
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
    }
}
